package com.service.impl;

import com.dao.StatisticMapper;
import com.dao.VenderDeviceMapper;
import com.model.Orders;
import com.model.VenderDevice;
import com.result.Result;
import com.service.StatisticService;
import com.utils.CurrentLoginUserAgent;
import com.utils.DateUtil;
import com.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.testng.collections.Lists;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author: chihaojie
 * @Date: 2019/6/22 16:15
 * @Version 1.0
 * @Note
 */

@Slf4j
@Service
public class StatisticServiceImpl implements StatisticService {


    @Autowired
    private StatisticMapper statisticMapper;
    @Autowired
    private VenderDeviceMapper deviceMapper;


    @Override
    public Result<WholeDailySaleVO> getWholeDailySaleStatistic(Date day, HttpServletRequest request, HttpServletResponse response) {
        Result<WholeDailySaleVO> result = new Result<WholeDailySaleVO>();
        WholeDailySaleVO dailySale = new WholeDailySaleVO();
        Long businessId = CurrentLoginUserAgent.getBusinessId();
        Date morning = DateUtil.getMorning(day);
        Date nextMorning = DateUtil.getNextMorning(day);
        List<Orders> ordersList = statisticMapper.selectAgentWholeSaleStatisticByStarttimeAndEndtime(businessId + "", morning, nextMorning);
        //进行统计计算
        if (ObjectUtils.isEmpty(ordersList)) {
            dailySale.setAgent(businessId);
            dailySale.setDay(day);
            dailySale.setSaleMoney(new Double("0.00"));
            dailySale.setSaleNum(0);
            return result.ok(dailySale);
        }
        BigDecimal tempSaleMoney = new BigDecimal("0.00");
        for (Orders order : ordersList) {
            tempSaleMoney = tempSaleMoney.add(order.getTotalPayMoney());
        }
        dailySale.setAgent(businessId);
        dailySale.setDay(day);
        dailySale.setSaleMoney(tempSaleMoney.doubleValue());
        dailySale.setSaleNum(ordersList.size());
        return result.ok(dailySale);
    }

    @Override
    public Result<WholeMonthSaleVO> getWholeMonthSaleStatistic(Long month, HttpServletRequest request, HttpServletResponse response) {
        Result<WholeMonthSaleVO> result = new Result<WholeMonthSaleVO>();
        Long businessId = CurrentLoginUserAgent.getBusinessId();
        WholeMonthSaleVO monthSale = new WholeMonthSaleVO();
        //取出本月第一天
        Date date = new Date(month);
        Date firstDayMorningOfMonth = DateUtil.getFirstDayMorningOfMonth(date);
        Date firstDayMorningOfNextMonth = DateUtil.getFirstDayMorningOfNextMonth(date);
        List<Orders> ordersList = statisticMapper.selectAgentWholeSaleStatisticByStarttimeAndEndtime(businessId + "", firstDayMorningOfMonth, firstDayMorningOfNextMonth);
        //进行统计计算
        if (ObjectUtils.isEmpty(ordersList)) {
            monthSale.setAgent(businessId);
            monthSale.setMonth(date);
            monthSale.setSaleMoney(new Double("0.00"));
            monthSale.setSaleNum(0);
            return result.ok(monthSale);
        }
        BigDecimal tempSaleMoney = new BigDecimal("0.00");
        for (Orders order : ordersList) {
            tempSaleMoney = tempSaleMoney.add(order.getTotalPayMoney());
        }
        monthSale.setAgent(businessId);
        monthSale.setMonth(date);
        monthSale.setSaleMoney(tempSaleMoney.doubleValue());
        monthSale.setSaleNum(ordersList.size());
        return result.ok(monthSale);
    }

    @Override
    public Result<SingleDeviceDaySaleVO> getSingleDeviceDaySaleStatistic(Long deviceId, Date day, HttpServletRequest request, HttpServletResponse response) {
        Result<SingleDeviceDaySaleVO> result = new Result<SingleDeviceDaySaleVO>();
        Long businessId = CurrentLoginUserAgent.getBusinessId();
        SingleDeviceDaySaleVO daySaleVO = new SingleDeviceDaySaleVO();
        Date morning = DateUtil.getMorning(day);
        Date nextMorning = DateUtil.getNextMorning(day);
        List<Orders> ordersList = statisticMapper.selectSingleDeviceSaleStatisticByStarttimeAndEndtime(businessId + "", deviceId, morning, nextMorning);
        //进行统计计算
        if (ObjectUtils.isEmpty(ordersList)) {
            daySaleVO.setDeviceId(deviceId);
            daySaleVO.setDay(day);
            daySaleVO.setSaleMoney(new Double("0.00"));
            daySaleVO.setSaleNum(0);
            return result.ok(daySaleVO);
        }
        BigDecimal tempSaleMoney = new BigDecimal("0.00");
        for (Orders order : ordersList) {
            tempSaleMoney = tempSaleMoney.add(order.getTotalPayMoney());
        }
        daySaleVO.setDeviceId(deviceId);
        daySaleVO.setDay(day);
        daySaleVO.setSaleMoney(tempSaleMoney.doubleValue());
        daySaleVO.setSaleNum(ordersList.size());
        return result.ok(daySaleVO);
    }

    @Override
    public Result<SingleDeviceMonthSaleVO> getSingleDeviceMonthSaleStatistic(Long deviceId, Long month, HttpServletRequest request, HttpServletResponse response) {
        Result<SingleDeviceMonthSaleVO> result = new Result<SingleDeviceMonthSaleVO>();
        Long businessId = CurrentLoginUserAgent.getBusinessId();
        SingleDeviceMonthSaleVO monthSale = new SingleDeviceMonthSaleVO();
        //取出本月第一天
        Date date = new Date(month);
        Date firstDayMorningOfMonth = DateUtil.getFirstDayMorningOfMonth(date);
        Date firstDayMorningOfNextMonth = DateUtil.getFirstDayMorningOfNextMonth(date);
        List<Orders> ordersList = statisticMapper.selectSingleDeviceSaleStatisticByStarttimeAndEndtime(businessId + "", deviceId, firstDayMorningOfMonth, firstDayMorningOfNextMonth);
        //进行统计计算
        if (ObjectUtils.isEmpty(ordersList)) {
            monthSale.setDeviceId(deviceId);
            monthSale.setMonth(date);
            monthSale.setSaleMoney(new Double("0.00"));
            monthSale.setSaleNum(0);
            return result.ok(monthSale);
        }
        BigDecimal tempSaleMoney = new BigDecimal("0.00");
        for (Orders order : ordersList) {
            tempSaleMoney = tempSaleMoney.add(order.getTotalPayMoney());
        }
        monthSale.setDeviceId(deviceId);
        monthSale.setMonth(date);
        monthSale.setSaleMoney(tempSaleMoney.doubleValue());
        monthSale.setSaleNum(ordersList.size());
        return result.ok(monthSale);
    }


    @Override
    public Result<SingleHospitalDaySaleVO> getSingleHospitalDaySaleStatistic(Long hospitalId, Date date, HttpServletRequest request, HttpServletResponse response) {
        Result<SingleHospitalDaySaleVO> result = new Result<SingleHospitalDaySaleVO>();
        Long businessId = CurrentLoginUserAgent.getBusinessId();
        SingleHospitalDaySaleVO daySaleVO = new SingleHospitalDaySaleVO();
        Date morning = DateUtil.getMorning(date);
        Date nextMorning = DateUtil.getNextMorning(date);
        List<Orders> ordersList = statisticMapper.selectAgentSingleHospitalDaySaleStatisticByStarttimeAndEndtime(businessId + "", hospitalId, morning, nextMorning);
        //进行统计计算
        if (ObjectUtils.isEmpty(ordersList)) {
            daySaleVO.setHospitalId(hospitalId);
            daySaleVO.setDay(date);
            daySaleVO.setSaleMoney(new Double("0.00"));
            daySaleVO.setSaleNum(0);
            return result.ok(daySaleVO);
        }
        BigDecimal tempSaleMoney = new BigDecimal("0.00");
        for (Orders order : ordersList) {
            tempSaleMoney = tempSaleMoney.add(order.getTotalPayMoney());
        }
        daySaleVO.setHospitalId(hospitalId);
        daySaleVO.setDay(date);
        daySaleVO.setSaleMoney(tempSaleMoney.doubleValue());
        daySaleVO.setSaleNum(ordersList.size());
        return result.ok(daySaleVO);
    }

    @Override
    public Result<SingleHospitalMonthSaleVO> getSingleHospitalMonthSaleStatistic(Long hospitalId, Long month, HttpServletRequest request, HttpServletResponse response) {
        Result<SingleHospitalMonthSaleVO> result = new Result<SingleHospitalMonthSaleVO>();
        Long businessId = CurrentLoginUserAgent.getBusinessId();
        SingleHospitalMonthSaleVO monthSale = new SingleHospitalMonthSaleVO();
        //取出本月第一天
        Date date = new Date(month);
        Date firstDayMorningOfMonth = DateUtil.getFirstDayMorningOfMonth(date);
        Date firstDayMorningOfNextMonth = DateUtil.getFirstDayMorningOfNextMonth(date);
        List<Orders> ordersList = statisticMapper.selectAgentSingleHospitalDaySaleStatisticByStarttimeAndEndtime(businessId + "", hospitalId, firstDayMorningOfMonth, firstDayMorningOfNextMonth);
        //进行统计计算
        if (ObjectUtils.isEmpty(ordersList)) {
            monthSale.setHospitalId(hospitalId);
            monthSale.setMonth(date);
            monthSale.setSaleMoney(new Double("0.00"));
            monthSale.setSaleNum(0);
            return result.ok(monthSale);
        }
        BigDecimal tempSaleMoney = new BigDecimal("0.00");
        for (Orders order : ordersList) {
            tempSaleMoney = tempSaleMoney.add(order.getTotalPayMoney());
        }
        monthSale.setHospitalId(hospitalId);
        monthSale.setMonth(date);
        monthSale.setSaleMoney(tempSaleMoney.doubleValue());
        monthSale.setSaleNum(ordersList.size());
        return result.ok(monthSale);
    }

    @Override
    public Result<ProductSKUDaySaleVO> getProductSKUDaySaleStatistic(Long productId, Date date, HttpServletRequest request, HttpServletResponse response) {
        Result<ProductSKUDaySaleVO> result = new Result<ProductSKUDaySaleVO>();
        Long businessId = CurrentLoginUserAgent.getBusinessId();
        ProductSKUDaySaleVO daySaleVO = new ProductSKUDaySaleVO();
        Date morning = DateUtil.getMorning(date);
        Date nextMorning = DateUtil.getNextMorning(date);
        List<Orders> ordersList = statisticMapper.selectAgentProductSKUSaleStatisticByStarttimeAndEndtime(businessId + "", productId, morning, nextMorning);
        //进行统计计算
        if (ObjectUtils.isEmpty(ordersList)) {
            daySaleVO.setProductId(productId);
            daySaleVO.setDay(date);
            daySaleVO.setSaleMoney(new Double("0.00"));
            daySaleVO.setSaleNum(0);
            return result.ok(daySaleVO);
        }
        BigDecimal tempSaleMoney = new BigDecimal("0.00");
        for (Orders order : ordersList) {
            tempSaleMoney = tempSaleMoney.add(order.getTotalPayMoney());
        }
        daySaleVO.setProductId(productId);
        daySaleVO.setDay(date);
        daySaleVO.setSaleMoney(tempSaleMoney.doubleValue());
        daySaleVO.setSaleNum(ordersList.size());
        return result.ok(daySaleVO);
    }

    @Override
    public Result<ProductSKUMonthSaleVO> getProductSKUMonthSaleStatistic(Long productId, Long month, HttpServletRequest request, HttpServletResponse response) {
        Result<ProductSKUMonthSaleVO> result = new Result<ProductSKUMonthSaleVO>();
        Long businessId = CurrentLoginUserAgent.getBusinessId();
        ProductSKUMonthSaleVO monthSale = new ProductSKUMonthSaleVO();
        //取出本月第一天
        Date date = new Date(month);
        Date firstDayMorningOfMonth = DateUtil.getFirstDayMorningOfMonth(date);
        Date firstDayMorningOfNextMonth = DateUtil.getFirstDayMorningOfNextMonth(date);
        List<Orders> ordersList = statisticMapper.selectAgentSingleHospitalDaySaleStatisticByStarttimeAndEndtime(businessId + "", productId, firstDayMorningOfMonth, firstDayMorningOfNextMonth);
        //进行统计计算
        if (ObjectUtils.isEmpty(ordersList)) {
            monthSale.setProductId(productId);
            monthSale.setMonth(date);
            monthSale.setSaleMoney(new Double("0.00"));
            monthSale.setSaleNum(0);
            return result.ok(monthSale);
        }
        BigDecimal tempSaleMoney = new BigDecimal("0.00");
        for (Orders order : ordersList) {
            tempSaleMoney = tempSaleMoney.add(order.getTotalPayMoney());
        }
        monthSale.setProductId(productId);
        monthSale.setMonth(date);
        monthSale.setSaleMoney(tempSaleMoney.doubleValue());
        monthSale.setSaleNum(ordersList.size());
        return result.ok(monthSale);
    }


    @Override
    public Result<List<ProductSKUSaleVO>> getAgentProductSKUSaleStatistic(Long productId, Long startTime, Long entTime) {

        return null;
    }

    @Override
    public Result<List<ProductSKUSaleVO>> getAgentAllProductSKUSaleStatistic(Long startTime, Long entTime) {
        Result<List<ProductSKUSaleVO>> result = new Result<List<ProductSKUSaleVO>>();
        Long businessId =CurrentLoginUserAgent.getBusinessId();
        List<ProductSKUSaleVO> skuSaleList = Lists.newArrayList();
        Date date1 = new Date(startTime);
        Date date2 = new Date(entTime);
        System.out.println(date1);
        System.out.println(date2);
        List<ProductSKUSaleVO> tempList = statisticMapper.selectAgentAllProductSaleByStarttimeAndEndtime(businessId + "", date1, date2);
        System.out.println("===" + tempList);
        //ProductSKUSaleVO temp=  statisticMapper.selectAgentSingleProductSaleByStarttimeAndEndtime(businessId,productId,date1,date2);
        if (!ObjectUtils.isEmpty(tempList)) {
            return result.ok(tempList);
        }
        return result.ok(skuSaleList);
    }


    @Override
    public Result<List<ProductSKUSaleVO>> getAgentSingleSKUDaySaleStatistic(Long productId, Long startTime, Long entTime) {
        Result<List<ProductSKUSaleVO>> result = new Result<List<ProductSKUSaleVO>>();
        List<ProductSKUSaleVO> daySkuSaleList = Lists.newArrayList();
        Long businessId =CurrentLoginUserAgent.getBusinessId();
        Date date1 = new Date(startTime);
        Date date2 = new Date(entTime);
        Date startDate = DateUtil.getMorning(date1);
        Date endDate = DateUtil.getMorning(date2);
        //判断这段时间段内有多少天
        int dayCount = DateUtil.daysBetween(startDate, endDate);
        System.out.println("间隔了多少天：" + dayCount);
        if (dayCount < 1) {
            Date tempEnd = DateUtil.getNextMorning(startDate);
            ProductSKUSaleVO temp = statisticMapper.selectAgentSingleProductSaleByStarttimeAndEndtime(businessId + "", productId, startDate, tempEnd);
            ProductSKUSaleVO  skuSaleVO=new ProductSKUSaleVO();
            if(!ObjectUtils.isEmpty(temp)){
                skuSaleVO.setSaleNum(temp.getSaleNum());
                skuSaleVO.setSaleMoney(temp.getSaleMoney());
                skuSaleVO.setDate(startDate);
                daySkuSaleList.add(skuSaleVO);
            }
            return result.ok(daySkuSaleList);
        }
        Date tempStart = startDate;
        for (int i = 0; i <= dayCount; i++) {
            Date tempEnd = DateUtil.getNextMorning(tempStart);
            ProductSKUSaleVO temp = statisticMapper.selectAgentSingleProductSaleByStarttimeAndEndtime(businessId + "", productId, tempStart, tempEnd);
            if(!ObjectUtils.isEmpty(temp)){
                ProductSKUSaleVO  skuSaleVO=new ProductSKUSaleVO();
                skuSaleVO.setSaleNum(temp.getSaleNum());
                skuSaleVO.setSaleMoney(temp.getSaleMoney());
                skuSaleVO.setProductId(productId);
                skuSaleVO.setDate(tempStart);
                daySkuSaleList.add(skuSaleVO);
            }
            tempStart = DateUtil.getNextMorning(tempStart);
        }
        return result.ok(daySkuSaleList);
    }

    /**
     * 月销售统计
     *
     * @param productId
     * @param startTime
     * @param entTime
     * @return
     */
    @Override
    public Result<List<ProductSKUMonthSaleVO>> getAgentSingleSKUMonthSaleStatistic(Long productId, Long startTime, Long entTime) {
        Result<List<ProductSKUMonthSaleVO>> result = new Result<List<ProductSKUMonthSaleVO>>();
        List<ProductSKUMonthSaleVO> list = Lists.newArrayList();
        Long businessId =CurrentLoginUserAgent.getBusinessId();
        Date date1 = new Date(startTime);
        Date date2 = new Date(entTime);
        //间隔了多少个月
        Integer month1 = DateUtil.abstractMonthFromDate(date1);
        Integer month2 = DateUtil.abstractMonthFromDate(date2);
        Integer monthCount = month2 - month1;
        if (monthCount < 1) {
            Date tempStart = DateUtil.getFirstDayMorningOfMonth(date1);
            Date tempEnd = DateUtil.getFirstDayMorningOfNextMonth(date1);
            ProductSKUSaleVO temp = statisticMapper.selectAgentSingleProductSaleByStarttimeAndEndtime(businessId + "", productId, tempStart, tempEnd);
            if(!ObjectUtils.isEmpty(temp)){
                ProductSKUMonthSaleVO monthSaleVO = new ProductSKUMonthSaleVO();
                monthSaleVO.setMonth(tempStart);
                monthSaleVO.setProductId(productId);
                monthSaleVO.setSaleNum(temp.getSaleNum());
                monthSaleVO.setSaleMoney(temp.getSaleMoney());
                list.add(monthSaleVO);
            }
            return result.ok(list);
        }
        Date tempStart = DateUtil.getFirstDayMorningOfMonth(date1);
        for (int i = 0; i <= monthCount; i++) {
            Date tempEnd = DateUtil.getFirstDayMorningOfNextMonth(tempStart);
            ProductSKUSaleVO temp = statisticMapper.selectAgentSingleProductSaleByStarttimeAndEndtime(businessId + "", productId, tempStart, tempEnd);
            if(!ObjectUtils.isEmpty(temp)){
                ProductSKUMonthSaleVO monthSaleVO = new ProductSKUMonthSaleVO();
                monthSaleVO.setMonth(tempStart);
                monthSaleVO.setProductId(productId);
                monthSaleVO.setSaleNum(temp.getSaleNum());
                monthSaleVO.setSaleMoney(temp.getSaleMoney());
                list.add(monthSaleVO);
            }
            tempStart = DateUtil.getFirstDayMorningOfNextMonth(tempStart);
        }
        return result.ok(list);
    }


    @Override
    public Result<List<SingleDeviceSaleVO>> getAgentAllDeviceSaleStatistic(Long startTime, Long entTime) {
        Result<List<SingleDeviceSaleVO>> result = new Result<List<SingleDeviceSaleVO>>();
        List<SingleDeviceSaleVO> list = Lists.newArrayList();
        Long businessId =CurrentLoginUserAgent.getBusinessId();
        Date date1 = new Date(startTime);
        Date date2 = new Date(entTime);
        List<SingleDeviceSaleVO> saleVOList = statisticMapper.seAgentAllDeviceSaleByStarttimeAndEndtime(businessId + "", date1, date2);
        if(!ObjectUtils.isEmpty(saleVOList)){
            //填充设备名称
            //TODO
            for (SingleDeviceSaleVO deviceSale: saleVOList) {
                VenderDevice device = deviceMapper.selectByPrimaryKey(deviceSale.getDeviceId());
                if(!ObjectUtils.isEmpty(device)){
                    deviceSale.setDeviceName(device.getDeviceName());
                }
            }

            return result.ok(saleVOList);
        }
        return result.ok(list);
    }


    /**
     * 天
     * @param deviceId
     * @param startTime
     * @param entTime
     * @return
     */
    @Override
    public Result<List<SingleDeviceDaySaleVO>> getAgentSingleDeviceDaySaleStatistic(Long deviceId, Long startTime, Long entTime) {
        Result<List<SingleDeviceDaySaleVO>> result = new Result<List<SingleDeviceDaySaleVO>>();
        List<SingleDeviceDaySaleVO> deviceDaySaleVOList = Lists.newArrayList();
        Long businessId =CurrentLoginUserAgent.getBusinessId();
        Date date1 = new Date(startTime);
        Date date2 = new Date(entTime);
        Date startDate = DateUtil.getMorning(date1);
        Date endDate = DateUtil.getMorning(date2);
        //判断这段时间段内有多少天
        int dayCount = DateUtil.daysBetween(startDate, endDate);
        System.out.println("间隔了多少天：" + dayCount);
        if (dayCount < 1) {
            Date tempEnd = DateUtil.getNextMorning(startDate);
            SingleDeviceDaySaleVO temp = statisticMapper.selectAgentSingleDeviceSaleByStarttimeAndEndtime(businessId + "", deviceId, startDate, tempEnd);
            SingleDeviceDaySaleVO  daySaleVO=new SingleDeviceDaySaleVO();
            if(!ObjectUtils.isEmpty(temp)){
                VenderDevice venderDevice = deviceMapper.selectByPrimaryKey(deviceId);
                if(!ObjectUtils.isEmpty(venderDevice)){
                    daySaleVO.setDeviceName(venderDevice.getDeviceName());
                }
                daySaleVO.setDeviceId(deviceId);
                daySaleVO.setSaleMoney(temp.getSaleMoney());
                daySaleVO.setSaleNum(temp.getSaleNum());
                daySaleVO.setDay(startDate);
                deviceDaySaleVOList.add(daySaleVO);
            }
            return result.ok(deviceDaySaleVOList);
        }
        Date tempStart = startDate;
        for (int i = 0; i <= dayCount; i++) {
            Date tempEnd = DateUtil.getNextMorning(tempStart);
            SingleDeviceDaySaleVO temp = statisticMapper.selectAgentSingleDeviceSaleByStarttimeAndEndtime(businessId + "", deviceId, tempStart, tempEnd);
            if(!ObjectUtils.isEmpty(temp)){
                SingleDeviceDaySaleVO  daySaleVO=new SingleDeviceDaySaleVO();
                VenderDevice venderDevice = deviceMapper.selectByPrimaryKey(deviceId);
                if(!ObjectUtils.isEmpty(venderDevice)){
                    daySaleVO.setDeviceName(venderDevice.getDeviceName());
                }
                daySaleVO.setDeviceId(deviceId);
                daySaleVO.setSaleMoney(temp.getSaleMoney());
                daySaleVO.setSaleNum(temp.getSaleNum());
                daySaleVO.setDay(tempStart);
                deviceDaySaleVOList.add(daySaleVO);
            }
            tempStart = DateUtil.getNextMorning(tempStart);
        }
        return result.ok(deviceDaySaleVOList);
    }

    @Override
    public Result<List<SingleDeviceMonthSaleVO>> getAgentSingleDeviceMonthSaleStatistic(Long deviceId, Long startTime, Long entTime) {
        Result<List<SingleDeviceMonthSaleVO>> result = new Result<List<SingleDeviceMonthSaleVO>>();
        List<SingleDeviceMonthSaleVO> list = Lists.newArrayList();
        Long businessId =CurrentLoginUserAgent.getBusinessId();
        Date date1 = new Date(startTime);
        Date date2 = new Date(entTime);
        //间隔了多少个月
        Integer month1 = DateUtil.abstractMonthFromDate(date1);
        Integer month2 = DateUtil.abstractMonthFromDate(date2);
        Integer monthCount = month2 - month1;
        if (monthCount < 1) {
            Date tempStart = DateUtil.getFirstDayMorningOfMonth(date1);
            Date tempEnd = DateUtil.getFirstDayMorningOfNextMonth(date1);
            SingleDeviceDaySaleVO temp = statisticMapper.selectAgentSingleDeviceSaleByStarttimeAndEndtime(businessId + "", deviceId, tempStart, tempEnd);
            SingleDeviceMonthSaleVO monthSaleVO = new SingleDeviceMonthSaleVO();
            if(!ObjectUtils.isEmpty(temp)){
                VenderDevice venderDevice = deviceMapper.selectByPrimaryKey(deviceId);
                if(!ObjectUtils.isEmpty(venderDevice)){
                    monthSaleVO.setDeviceName(venderDevice.getDeviceName());
                }
                monthSaleVO.setDeviceId(deviceId);
                monthSaleVO.setSaleNum(temp.getSaleNum());
                monthSaleVO.setSaleMoney(temp.getSaleMoney());
                monthSaleVO.setMonth(tempStart);
                list.add(monthSaleVO);
            }
            return result.ok(list);
        }
        Date tempStart = DateUtil.getFirstDayMorningOfMonth(date1);
        for (int i = 0; i <= monthCount; i++) {
            Date tempEnd = DateUtil.getFirstDayMorningOfNextMonth(tempStart);
            SingleDeviceDaySaleVO temp = statisticMapper.selectAgentSingleDeviceSaleByStarttimeAndEndtime(businessId + "", deviceId, tempStart, tempEnd);
            SingleDeviceMonthSaleVO monthSaleVO = new SingleDeviceMonthSaleVO();
            if(!ObjectUtils.isEmpty(temp)){
                VenderDevice venderDevice = deviceMapper.selectByPrimaryKey(deviceId);
                if(!ObjectUtils.isEmpty(venderDevice)){
                    monthSaleVO.setDeviceName(venderDevice.getDeviceName());
                }
                monthSaleVO.setDeviceId(deviceId);
                monthSaleVO.setSaleNum(temp.getSaleNum());
                monthSaleVO.setSaleMoney(temp.getSaleMoney());
                monthSaleVO.setMonth(tempStart);
                list.add(monthSaleVO);
            }
            tempStart = DateUtil.getFirstDayMorningOfNextMonth(tempStart);
        }
        return result.ok(list);
    }


    /**
     * 医院
     */
    @Override
    public Result<List<SingleHospitalSaleVO>> getAgentAllHospitalSaleStatistic(Long startTime, Long entTime) {
        Result<List<SingleHospitalSaleVO>>  result=new Result<List<SingleHospitalSaleVO>>();
        List<SingleHospitalSaleVO> list = Lists.newArrayList();
        Long businessId =CurrentLoginUserAgent.getBusinessId();
        Date date1 = new Date(startTime);
        Date date2 = new Date(entTime);
        List<SingleHospitalSaleVO> saleVOList = statisticMapper.seAgentAllHospitalSaleByStarttimeAndEndtime(businessId + "", date1, date2);
        if(!ObjectUtils.isEmpty(saleVOList)){
            return result.ok(saleVOList);
        }
        return result.ok(list);
    }

    /**
     * 单个医院每天
     */
    @Override
    public Result<List<SingleHospitalDaySaleVO>> getAgentSingleHospitalDaySaleStatistic(Long hospitalId, Long startTime, Long entTime) {
        Result<List<SingleHospitalDaySaleVO>> result = new Result<List<SingleHospitalDaySaleVO>>();
        List<SingleHospitalDaySaleVO> hospitalDaySaleVOList = Lists.newArrayList();
        Long businessId =CurrentLoginUserAgent.getBusinessId();
        Date date1 = new Date(startTime);
        Date date2 = new Date(entTime);
        Date startDate = DateUtil.getMorning(date1);
        Date endDate = DateUtil.getMorning(date2);
        //判断这段时间段内有多少天
        int dayCount = DateUtil.daysBetween(startDate, endDate);
        System.out.println("间隔了多少天：" + dayCount);
        if (dayCount < 1) {
            Date tempEnd = DateUtil.getNextMorning(startDate);
            SingleHospitalDaySaleVO temp = statisticMapper.selectAgentSingleHospitalSaleByStarttimeAndEndtime(businessId + "", hospitalId, startDate, tempEnd);
            if(!ObjectUtils.isEmpty(temp)){
                SingleHospitalDaySaleVO  daySaleVO=new SingleHospitalDaySaleVO();
                daySaleVO.setHospitalName(temp.getHospitalName());
                daySaleVO.setSaleNum(temp.getSaleNum());
                daySaleVO.setSaleMoney(temp.getSaleMoney());
                daySaleVO.setDay(startDate);
                daySaleVO.setHospitalId(hospitalId);
                hospitalDaySaleVOList.add(daySaleVO);
            }
            return result.ok(hospitalDaySaleVOList);
        }
        Date tempStart = startDate;
        for (int i = 0; i <= dayCount; i++) {
            Date tempEnd = DateUtil.getNextMorning(tempStart);
            SingleHospitalDaySaleVO temp = statisticMapper.selectAgentSingleHospitalSaleByStarttimeAndEndtime(businessId + "", hospitalId, tempStart, tempEnd);
            SingleHospitalDaySaleVO  daySaleVO=new SingleHospitalDaySaleVO();
            if(!ObjectUtils.isEmpty(temp)){
                daySaleVO.setHospitalId(temp.getHospitalId());
                daySaleVO.setHospitalName(temp.getHospitalName());
                daySaleVO.setSaleNum(temp.getSaleNum());
                daySaleVO.setSaleMoney(temp.getSaleMoney());
                daySaleVO.setDay(tempStart);
                hospitalDaySaleVOList.add(daySaleVO);
            }
            tempStart = DateUtil.getNextMorning(tempStart);
        }
        return result.ok(hospitalDaySaleVOList);
    }

    @Override
    public Result<List<SingleHospitalMonthSaleVO>> getAgentSingleHospitalMonthSaleStatistic(Long hospitalId, Long startTime, Long entTime) {
        Result<List<SingleHospitalMonthSaleVO>> result = new Result<List<SingleHospitalMonthSaleVO>>();
        List<SingleHospitalMonthSaleVO> list = Lists.newArrayList();
        Long businessId =CurrentLoginUserAgent.getBusinessId();
        Date date1 = new Date(startTime);
        Date date2 = new Date(entTime);
        //间隔了多少个月
        Integer month1 = DateUtil.abstractMonthFromDate(date1);
        Integer month2 = DateUtil.abstractMonthFromDate(date2);
        Integer monthCount = month2 - month1;
        if (monthCount < 1) {
            Date tempStart = DateUtil.getFirstDayMorningOfMonth(date1);
            Date tempEnd = DateUtil.getFirstDayMorningOfNextMonth(date1);
            SingleHospitalDaySaleVO temp = statisticMapper.selectAgentSingleHospitalSaleByStarttimeAndEndtime(businessId + "", hospitalId, tempStart, tempEnd);
            if(!ObjectUtils.isEmpty(temp)){
                SingleHospitalMonthSaleVO monthSaleVO = new SingleHospitalMonthSaleVO();
                monthSaleVO.setMonth(tempStart);
                monthSaleVO.setHospitalId(temp.getHospitalId());
                monthSaleVO.setHospitalName(temp.getHospitalName());
                monthSaleVO.setSaleNum(temp.getSaleNum());
                monthSaleVO.setSaleMoney(temp.getSaleMoney());
                list.add(monthSaleVO);
            }
            return result.ok(list);
        }
        Date tempStart = DateUtil.getFirstDayMorningOfMonth(date1);
        for (int i = 0; i <= monthCount; i++) {
            Date tempEnd = DateUtil.getFirstDayMorningOfNextMonth(tempStart);
            SingleHospitalDaySaleVO temp = statisticMapper.selectAgentSingleHospitalSaleByStarttimeAndEndtime(businessId + "", hospitalId, tempStart, tempEnd);
            SingleHospitalMonthSaleVO monthSaleVO = new SingleHospitalMonthSaleVO();
            if(!ObjectUtils.isEmpty(temp)){
                monthSaleVO.setHospitalId(temp.getHospitalId());
                monthSaleVO.setHospitalName(temp.getHospitalName());
                monthSaleVO.setSaleNum(temp.getSaleNum());
                monthSaleVO.setSaleMoney(temp.getSaleMoney());
                monthSaleVO.setMonth(tempStart);
                list.add(monthSaleVO);
            }
            tempStart = DateUtil.getFirstDayMorningOfNextMonth(tempStart);
        }
        return result.ok(list);
    }


    @Override
    public Result<List<WholeDailySaleVO>> getDailySaleStatistic(Long startTime, Long entTime, HttpServletRequest request, HttpServletResponse response) {
        Result<List<WholeDailySaleVO>> result = new Result<List<WholeDailySaleVO>>();
        List<WholeDailySaleVO> daySaleVOList = Lists.newArrayList();
        Long businessId =CurrentLoginUserAgent.getBusinessId();
        Date date1 = new Date(startTime);
        Date date2 = new Date(entTime);
        Date startDate = DateUtil.getMorning(date1);
        Date endDate = DateUtil.getMorning(date2);
        int dayCount = DateUtil.daysBetween(startDate, endDate);
        System.out.println("间隔了多少天：" + dayCount);
        if (dayCount < 1) {
            Date tempEnd = DateUtil.getNextMorning(startDate);
            //查询一段时间的销售情况
            WholeDailySaleVO  dailySaleVO=statisticMapper.selectAgentSaleStatisticByStarttimeAndEndtime(businessId+"",startDate,tempEnd);
            if(!ObjectUtils.isEmpty(dailySaleVO)){
                daySaleVOList.add(dailySaleVO);
            }
            return  result.ok(daySaleVOList);
        }
        Date tempStart = startDate;
        for (int i = 0; i <= dayCount; i++) {
            Date tempEnd = DateUtil.getNextMorning(tempStart);
            WholeDailySaleVO temp=statisticMapper.selectAgentSaleStatisticByStarttimeAndEndtime(businessId+"",tempStart,tempEnd);
            if(!ObjectUtils.isEmpty(temp)){
                WholeDailySaleVO  daySaleVO=new WholeDailySaleVO();
                daySaleVO.setAgent(businessId);
                daySaleVO.setSaleNum(temp.getSaleNum());
                daySaleVO.setSaleMoney(temp.getSaleMoney());
                daySaleVO.setDay(tempStart);
                daySaleVOList.add(daySaleVO);
            }
            tempStart = DateUtil.getNextMorning(tempStart);
        }
        return result.ok(daySaleVOList);
    }

    @Override
    public Result<List<WholeMonthSaleVO>> getMonthlySaleStatistic(Long startTime, Long entTime, HttpServletRequest request, HttpServletResponse response) {
        Result<List<WholeMonthSaleVO>> result = new Result<List<WholeMonthSaleVO>>();
        List<WholeMonthSaleVO> list = Lists.newArrayList();
        Long businessId =CurrentLoginUserAgent.getBusinessId();
        Date date1 = new Date(startTime);
        Date date2 = new Date(entTime);
        //间隔了多少个月
        Integer month1 = DateUtil.abstractMonthFromDate(date1);
        Integer month2 = DateUtil.abstractMonthFromDate(date2);
        Integer monthCount = month2 - month1;
        if (monthCount < 1) {
            Date tempStart = DateUtil.getFirstDayMorningOfMonth(date1);
            Date tempEnd = DateUtil.getFirstDayMorningOfNextMonth(date1);
            WholeDailySaleVO temp=statisticMapper.selectAgentSaleStatisticByStarttimeAndEndtime(businessId+"",tempStart,tempEnd);
            if(!ObjectUtils.isEmpty(temp)){
                WholeMonthSaleVO monthSaleVO = new WholeMonthSaleVO();
                monthSaleVO.setMonth(tempStart);
                monthSaleVO.setAgent(temp.getAgent());
                monthSaleVO.setSaleNum(temp.getSaleNum());
                monthSaleVO.setSaleMoney(temp.getSaleMoney());
                list.add(monthSaleVO);
            }
            return result.ok(list);
        }
        Date tempStart = DateUtil.getFirstDayMorningOfMonth(date1);
        for (int i = 0; i <= monthCount; i++) {
            Date tempEnd = DateUtil.getFirstDayMorningOfNextMonth(tempStart);
            WholeDailySaleVO temp=statisticMapper.selectAgentSaleStatisticByStarttimeAndEndtime(businessId+"",tempStart,tempEnd);
            if(!ObjectUtils.isEmpty(temp)){
                WholeMonthSaleVO monthSaleVO = new WholeMonthSaleVO();
                monthSaleVO.setMonth(tempStart);
                monthSaleVO.setAgent(businessId);
                monthSaleVO.setSaleNum(temp.getSaleNum());
                monthSaleVO.setSaleMoney(temp.getSaleMoney());
                list.add(monthSaleVO);
            }
            tempStart = DateUtil.getFirstDayMorningOfNextMonth(tempStart);
        }
        return result.ok(list);
    }
}
